package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class XingYouShanShuiRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {

        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if (config != null && config.getActivityInfo() != null) {
            addRecycleItemDataEndShowTime(config.getActivityInfo(), config.getSHANSHUI_EXCLUSIVE_DICE_ID(), 1, Collections.emptyList());
//            addRecycleItemDataEndShowTime(config.getActivityInfo(), config.getSHANSHUI_MECHANICAL_DICE_ID(), 1, Collections.emptyList());
            addRecycleItemDataEndShowTime(config.getActivityInfo(), config.getSHANSHUI_PASS_ID(), 1, Collections.emptyList());
            addRecycleItemDataEndShowTime(config.getActivityInfo(), config.getSHANSHUI_RED_ENVELOPE_ID(), 1, Collections.emptyList());

            //扩展图要回收成金页子
            List<String> paramList = new ArrayList<>();
            paramList.add(config.getSHANSHUI_EXPANSION_PLAN_RECYCLE());
            paramList.add(config.getActivityInfo().getActivityName());
            addRecycleItemDataEndShowTime(config.getActivityInfo(), config.getSHANSHUI_EXPANSION_PLAN_ID(), 2, paramList);

            //行囊道具
            paramList = new ArrayList<>();
            paramList.add(config.getSHANSHUI_BAG_RECLAIM());
            paramList.add(config.getActivityInfo().getActivityName());
            addRecycleItemDataEndShowTime(config.getActivityInfo(), config.getSHANSHUI_TIPS_ID(), 3, paramList);
        }
    }

    private void addRecycleItemDataEndTime(ActivityInfo activityInfo, int itemId, int recycleType, List<String> paramList) {
        long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleType, recycleTime, paramList);
    }

    private void addRecycleItemDataEndShowTime(ActivityInfo activityInfo, int itemId, int recycleType, List<String> paramList) {
        long recycleTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;
        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleType, recycleTime, paramList);
    }

    private void removeItem(long userId,ActivityItemRecycleData data){
        int itemId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, itemId);
        if (userBagItem == null || userBagItem.getNum() <= 0) {
            return;
        }
        removeItem(userBagItem);
    }

    @Override
    protected void initActivityType() {
        this.activityType = eActivityType.XingYouShanShui.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 1){
            removeItem(userId, data);
        }else if(data.getRecycleType() == 2){
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 转换其他道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = PropertyHelper.parseStringToProperty(data.getParamList().get(0));
                        reward.rideProperty(num);
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        }else if(data.getRecycleType() == 3){
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 转换其他道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = new Property();
                        for (long i = 0; i < num; i++) {
                            reward.addProperty(RandomWeightConfigHelper.getProperty(data.getParamList().get(0)));
                        }
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
